<template>
	<view class="goods-list">
		<view v-for="(item,i) in goodsList " :key="i" @click="gotoDetail(item)">
			<tool-shop :data="item"></tool-shop>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryParam:
				{
					///查询关键词
					query:'',
					///查询商品分类id
					cid:'',
					//页码值
					pageNum:1,
					//每页显示多少条
					pageSize:10
				},
				//商品列表
				goodsList:[],
				//总条数
				total:0,
				defaultPic:'',
				///是否正在请求数据
				isloading:false
			}
		},
		onLoad(option)
		{
			this.queryParam.cid=option.cid||''
			this.queryParam.query=option.query||'';
			this.getGoodsList()
			
		},
		methods: {
			///获取商品列表
			async  getGoodsList(cb)
			{
				this.isloading=true
				const res=await  uni.$http.get("/api/public/v1/goods/search",this.queryParam)
				this.isloading=false
				cb &&cb()
				if(res.statusCode!==200)
				{
					return uni.showMsg();
				}
				///为数据赋值，通过展开运算符的形式，进行新旧数据的拼接
				this.goodsList=[...this.goodsList,...res.data.message.goods];
				this.total=res.data.message.total;
			},
			gotoDetail(item)
			{
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
				})
			}
		},
		///下拉事件
		onPullDownRefresh() {
			this.queryParam.pageNum=1;
			this.total=0;
			this.isloading=false;
			this.goodsList=[];
			///重新发起请求
			this.getGoodsList(()=>uni.stopPullDownRefresh());
		},
		///上拉事件
		onReachBottom() {
			
			///数据加载完毕
			if(this.queryParam.pageNum*this.queryParam.pageSize>=this.total)
				return uni.$showMsg('数据加载完毕！')
			///下拉锁定
			if(this.isloading) 
				return 
			
			this.queryParam.pageNum+=1;
			this.getGoodsList()
		}
	}
</script>

<style lang="scss">
.goods-list
{

	
	.goods-item
	{
		display: flex;
		padding: 10px 5px;
		border-bottom: 1px solid #f0f0f0;
		.goods-item-left
		{
			margin-right: 5px;
			.goods-pic
			{
				width: 100px;
				height: 100px;
				display: block;
			}
		}
		.goods-item-right
		{
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			.goods-name
			{
				font-size: 13px;
			}
			.goods-info-box
			{
				.goods-price
				{
					color: #C00000;
					font-size: 16px;
				}
			}
		}
	}
}
</style>
